home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / rfc822.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-06  |  50.7 KB  |  1,594 lines

  1. /*
  2.  * Program:    RFC-822 routines (originally from SMTP)
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    27 July 1988
  13.  * Last Edited:    6 July 1993
  14.  *
  15.  * Sponsorship:    The original version of this work was developed in the
  16.  *        Symbolic Systems Resources Group of the Knowledge Systems
  17.  *        Laboratory at Stanford University in 1987-88, and was funded
  18.  *        by the Biomedical Research Technology Program of the National
  19.  *        Institutes of Health under grant number RR-00785.
  20.  *
  21.  * Original version Copyright 1988 by The Leland Stanford Junior University.
  22.  * Copyright 1993 by the University of Washington.
  23.  *
  24.  *  Permission to use, copy, modify, and distribute this software and its
  25.  * documentation for any purpose and without fee is hereby granted, provided
  26.  * that the above copyright notices appear in all copies and that both the
  27.  * above copyright notices and this permission notice appear in supporting
  28.  * documentation, and that the name of the University of Washington or The
  29.  * Leland Stanford Junior University not be used in advertising or publicity
  30.  * pertaining to distribution of the software without specific, written prior
  31.  * permission.  This software is made available "as is", and
  32.  * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY
  33.  * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE,
  34.  * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  35.  * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
  36.  * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY
  37.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  38.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  39.  * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF
  40.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41.  *
  42.  */
  43.  
  44.  
  45. #include <ctype.h>
  46. #include <stdio.h>
  47. #include <time.h>
  48. #include "mail.h"
  49. #include "osdep.h"
  50. #include "rfc822.h"
  51. #include "misc.h"
  52.  
  53. /* RFC-822 static data */
  54.  
  55.  
  56. /* Body formats constant strings, must match definitions in mail.h */
  57.  
  58. const char *body_types[] = {    /* defined body type strings */
  59.   "TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO",
  60.   "X-UNKNOWN"
  61. };
  62.  
  63.  
  64. const char *body_encodings[] = {/* defined body encoding strings */
  65.   "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "X-UNKNOWN"
  66. };
  67.  
  68.  
  69. /* Token delimiting special characters */
  70.  
  71.                 /* full RFC-822 specials */
  72. const char *rspecials =  "()<>@,;:\\\"[].";
  73.                 /* body token specials */
  74. const char *tspecials = " ()<>@,;:\\\"[]./?=";
  75.  
  76.  
  77. /* Once upon a time, CSnet had a mailer which assigned special semantics to
  78.  * dot in e-mail addresses.  For the sake of that mailer, dot was added to
  79.  * the RFC-822 definition of `specials', even though it had numerous bad side
  80.  * effects:
  81.  *   1)    It broke mailbox names on systems which had dots in user names, such as
  82.  *    Multics and TOPS-20.  RFC-822's syntax rules require that `Admin . MRC'
  83.  *    be considered equivalent to `Admin.MRC'.  Fortunately, few people ever
  84.  *    tried this in practice.
  85.  *   2) It required that all personal names with an initial be quoted, a widely
  86.  *    detested user interface misfeature.
  87.  *   3)    It made the parsing of host names be non-atomic for no good reason.
  88.  * To work around these problems, the following alternate specials lists are
  89.  * defined.  hspecials and wspecials are used in lieu of rspecials, and
  90.  * ptspecials are used in lieu of tspecials.  These alternate specials lists
  91.  * make the parser work a lot better in the real world.  It ain't politically
  92.  * correct, but it lets the users get their job done!
  93.  */
  94.  
  95.                 /* parse-host specials */
  96. const char *hspecials = " ()<>@,;:\\\"";
  97.                 /* parse-word specials */
  98. const char *wspecials = " ()<>@,;:\\\"[]";
  99.                 /* parse-token specials for parsing */
  100. const char *ptspecials = " ()<>@,;:\\\"[]/?=";
  101.  
  102. /* RFC822 writing routines */
  103.  
  104.  
  105. /* Write RFC822 header from message structure
  106.  * Accepts: scratch buffer to write into
  107.  *        message envelope
  108.  *        message body
  109.  */
  110.  
  111. void rfc822_header (header,env,body)
  112.     char *header;
  113.     ENVELOPE *env;
  114.     BODY *body;
  115. {
  116.   if (env->remail) {        /* if remailing */
  117.     long i = strlen (env->remail);
  118.                 /* flush extra blank line */
  119.     if (i > 4 && env->remail[i-4] == '\015') env->remail[i-2] = '\0';
  120.     strcpy (header,env->remail);/* start with remail header */
  121.   }
  122.   else *header = '\0';        /* else initialize header to null */
  123.   rfc822_header_line (&header,"Newsgroups",env,env->newsgroups);
  124.   rfc822_header_line (&header,"Date",env,env->date);
  125.   rfc822_address_line (&header,"From",env,env->from);
  126.   rfc822_address_line (&header,"Sender",env,env->sender);
  127.   rfc822_address_line (&header,"Reply-To",env,env->reply_to);
  128.   rfc822_header_line (&header,"Subject",env,env->subject);
  129.   rfc822_address_line (&header,"To",env,env->to);
  130.   rfc822_address_line (&header,"cc",env,env->cc);
  131. /* bcc's are never written...
  132.  * rfc822_address_line (&header,"bcc",env,env->bcc);
  133.  */
  134.   rfc822_header_line (&header,"In-Reply-To",env,env->in_reply_to);
  135.   rfc822_header_line (&header,"Message-ID",env,env->message_id);
  136.   if (body && !env->remail) {    /* not if remail or no body structure */
  137.     strcat (header,"MIME-Version: 1.0\015\012");
  138.     rfc822_write_body_header (&header,body);
  139.   }
  140.   strcat (header,"\015\012");    /* write terminating blank line */
  141. }
  142.  
  143. /* Write RFC822 address from header line
  144.  * Accepts: pointer to destination string pointer
  145.  *        pointer to header type
  146.  *        message to interpret
  147.  *        address to interpret
  148.  */
  149.  
  150. void rfc822_address_line (header,type,env,adr)
  151.     char **header;
  152.     char *type;
  153.     ENVELOPE *env;
  154.     ADDRESS *adr;
  155. {
  156.   char *t,tmp[MAILTMPLEN];
  157.   long i,len,n = 0;
  158.   char *s = (*header += strlen (*header));
  159.   if (adr) {            /* do nothing if no addresses */
  160.     if (env && env->remail) strcat (s,"ReSent-");
  161.     strcat (s,type);        /* write header name */
  162.     strcat (s,": ");
  163.     s += (len = strlen (s));    /* initial string length */
  164.     do {            /* run down address list */
  165.       *(t = tmp) = '\0';    /* initially empty string */
  166.                 /* start of group? */
  167.       if (adr->mailbox && !adr->host) {
  168.                 /* yes, write group name */
  169.     rfc822_cat (t,adr->mailbox,rspecials);
  170.     strcat (t,": ");    /* write group identifier */
  171.     n++;            /* in a group, suppress expansion */
  172.       }
  173.       else {            /* not start of group */
  174.     if (!adr->host && n) {    /* end of group? */
  175.       strcat (t,";");    /* write close delimiter */
  176.       n--;            /* no longer in a group */
  177.     }
  178.     else if (!n) {        /* only print if not inside a group */
  179.                 /* simple case? */
  180.       if (!(adr->personal || adr->adl)) rfc822_address (t,adr);
  181.       else {        /* no, must use phrase <route-addr> form */
  182.         if (adr->personal) rfc822_cat (t,adr->personal,rspecials);
  183.         strcat (t," <");    /* write address delimiter */
  184.                 /* write address */
  185.         rfc822_address (t,adr);
  186.         strcat (t,">");    /* closing delimiter */
  187.       }
  188.     }
  189.                 /* write delimiter for next recipient */
  190.     if (!n && adr->next && adr->next->mailbox) strcat (t,", ");
  191.       }
  192.                 /* if string would overflow */
  193.       if ((len += (i = strlen (t))) > 78) {
  194.     len = 4 + i;        /* continue it on a new line */
  195.     *s++ = '\015'; *s++ = '\012';
  196.     *s++ = ' '; *s++ = ' '; *s++ = ' '; *s++ = ' ';
  197.       }
  198.       while (*t) *s++ = *t++;    /* write this address */
  199.     } while (adr = adr->next);
  200.                 /* tie off header line */
  201.     *s++ = '\015'; *s++ = '\012'; *s = '\0';
  202.     *header = s;        /* set return value */
  203.   }
  204. }
  205.  
  206. /* Write RFC822 text from header line
  207.  * Accepts: pointer to destination string pointer
  208.  *        pointer to header type
  209.  *        message to interpret
  210.  *        pointer to text
  211.  */
  212.  
  213. void rfc822_header_line (header,type,env,text)
  214.     char **header;
  215.     char *type;
  216.     ENVELOPE *env;
  217.     char *text;
  218. {
  219.   if (text) sprintf ((*header += strlen (*header)),"%s%s: %s\015\012",
  220.              env->remail ? "ReSent-" : "",type,text);
  221. }
  222.  
  223.  
  224. /* Write RFC822 address
  225.  * Accepts: pointer to destination string
  226.  *        address to interpret
  227.  */
  228.  
  229. void rfc822_write_address (dest,adr)
  230.     char *dest;
  231.     ADDRESS *adr;
  232. {
  233.   while (adr) {
  234.                 /* start of group? */
  235.     if (adr->mailbox && !adr->host) {
  236.                 /* yes, write group name */
  237.       rfc822_cat (dest,adr->mailbox,rspecials);
  238.       strcat (dest,": ");    /* write group identifier */
  239.       adr = adr->next;        /* move to next address block */
  240.     }
  241.     else {            /* end of group? */
  242.       if (!adr->host) strcat (dest,";");
  243.                 /* simple case? */
  244.       else if (!(adr->personal || adr->adl)) rfc822_address (dest,adr);
  245.       else {            /* no, must use phrase <route-addr> form */
  246.     if (adr->personal) rfc822_cat (dest,adr->personal,rspecials);
  247.     strcat (dest," <");    /* write address delimiter */
  248.     rfc822_address (dest,adr);/* write address */
  249.     strcat (dest,">");    /* closing delimiter */
  250.       }
  251.                 /* delimit if there is one */
  252.       if ((adr = adr->next) && adr->mailbox) strcat (dest,", ");
  253.     }
  254.   }
  255. }
  256.  
  257. /* Write RFC822 route-address to string
  258.  * Accepts: pointer to destination string
  259.  *        address to interpret
  260.  */
  261.  
  262. void rfc822_address (dest,adr)
  263.     char *dest;
  264.     ADDRESS *adr;
  265. {
  266.   if (adr && adr->host) {    /* no-op if no address */
  267.     if (adr->adl) {        /* have an A-D-L? */
  268.       strcat (dest,adr->adl);
  269.       strcat (dest,":");
  270.     }
  271.                 /* write mailbox name */
  272.     rfc822_cat (dest,adr->mailbox,wspecials);
  273.     strcat (dest,"@");        /* host delimiter */
  274.     strcat (dest,adr->host);    /* write host name */
  275.   }
  276. }
  277.  
  278.  
  279. /* Concatenate RFC822 string
  280.  * Accepts: pointer to destination string
  281.  *        pointer to string to concatenate
  282.  *        list of special characters
  283.  */
  284.  
  285. void rfc822_cat (dest,src,specials)
  286.     char *dest;
  287.     char *src;
  288.     const char *specials;
  289. {
  290.   char *s;
  291.   if (strpbrk (src,specials)) {    /* any specials present? */
  292.     strcat (dest,"\"");        /* opening quote */
  293.                 /* truly bizarre characters in there? */
  294.     while (s = strpbrk (src,"\\\"")) {
  295.       strncat (dest,src,s-src);    /* yes, output leader */
  296.       strcat (dest,"\\");    /* quoting */
  297.       strncat (dest,s,1);    /* output the bizarre character */
  298.       src = ++s;        /* continue after the bizarre character */
  299.     }
  300.     if (*src) strcat (dest,src);/* output non-bizarre string */
  301.     strcat (dest,"\"");        /* closing quote */
  302.   }
  303.   else strcat (dest,src);    /* otherwise it's the easy case */
  304. }
  305.  
  306. /* Write body content header
  307.  * Accepts: pointer to destination string pointer
  308.  *        pointer to body to interpret
  309.  */
  310.  
  311. void rfc822_write_body_header (dst,body)
  312.     char **dst;
  313.     BODY *body;
  314. {
  315.   char *s;
  316.   PARAMETER *param = body->parameter;
  317.   sprintf (*dst += strlen (*dst),"Content-Type: %s",body_types[body->type]);
  318.   s = body->subtype ? body->subtype : rfc822_default_subtype (body->type);
  319.   sprintf (*dst += strlen (*dst),"/%s",s);
  320.   if (param) do {
  321.     sprintf (*dst += strlen (*dst),"; %s=",param->attribute);
  322.     rfc822_cat (*dst,param->value,tspecials);
  323.   } while (param = param->next);
  324.   else if (body->type == TYPETEXT) strcat (*dst,"; charset=US-ASCII");
  325.   strcpy (*dst += strlen (*dst),"\015\012");
  326.   if (body->encoding)        /* note: encoding 7BIT never output! */
  327.     sprintf (*dst += strlen (*dst),"Content-Transfer-Encoding: %s\015\012",
  328.          body_encodings[body->encoding]);
  329.   if (body->id) sprintf (*dst += strlen (*dst),"Content-ID: %s\015\012",
  330.              body->id);
  331.   if (body->description)
  332.     sprintf (*dst += strlen (*dst),"Content-Description: %s\015\012",
  333.          body->description);
  334. }
  335.  
  336.  
  337. /* Subtype defaulting (a no-no, but regretably necessary...)
  338.     a no-no;
  339.      but regretably necessary...;
  340.  * Accepts: type code
  341.  * Returns: default subtype name
  342.  */
  343.  
  344. char *rfc822_default_subtype (type)
  345.     unsigned short type;
  346. {
  347.   switch (type) {
  348.   case TYPETEXT:        /* default is TEXT/PLAIN */
  349.     return "PLAIN";
  350.   case TYPEMULTIPART:        /* default is MULTIPART/MIXED */
  351.     return "MIXED";
  352.   case TYPEMESSAGE:        /* default is MESSAGE/RFC822 */
  353.     return "RFC822";
  354.   case TYPEAPPLICATION:        /* default is APPLICATION/OCTET-STREAM */
  355.     return "OCTET-STREAM";
  356.   case TYPEAUDIO:        /* default is AUDIO/BASIC */
  357.     return "BASIC";
  358.   default:            /* others have no default subtype */
  359.     return "UNKNOWN";
  360.   }
  361. }
  362.  
  363. /* RFC822 parsing routines */
  364.  
  365.  
  366. /* Parse an RFC822 message
  367.  * Accepts: pointer to return envelope
  368.  *        pointer to return body
  369.  *        pointer to header
  370.  *        header byte count
  371.  *        pointer to body stringstruct
  372.  *        pointer to local host name
  373.  *        pointer to scratch buffer
  374.  */
  375.  
  376. void rfc822_parse_msg (en,bdy,s,i,bs,host,tmp)
  377.     ENVELOPE **en;
  378.     BODY **bdy;
  379.     char *s;
  380.     unsigned long i;
  381.                     STRING *bs;
  382.     char *host;
  383.     char *tmp;
  384. {
  385.   char c,*t,*d;
  386.   ENVELOPE *env = (*en = mail_newenvelope ());
  387.   BODY *body = bdy ? (*bdy = mail_newbody ()) : NIL;
  388.   long MIMEp = NIL;        /* flag that MIME semantics are in effect */
  389.   while (i > 0 && *s != '\n') {    /* until end of header */
  390.     t = tmp;            /* initialize buffer pointer */
  391.     c = ' ';            /* and previous character */
  392.     while (c) {            /* collect text until logical end of line */
  393.       switch (*s) {
  394.       case '\n':        /* newline, possible end of logical line */
  395.                 /* tie off unless next line starts with WS */
  396.     if (s[1] != ' ' && s[1] != '\t') *t = c = '\0';
  397.     break;
  398.       case '\015':        /* return, unlikely but just in case */
  399.       case '\t':        /* tab */
  400.       case ' ':            /* here for all forms of whitespace */
  401.                 /* insert whitespace if not already there */
  402.     if (c != ' ') *t++ = c = ' ';
  403.     break;
  404.       default:            /* all other characters */
  405.     *t++ = c = *s;        /* insert the character into the line */
  406.     break;
  407.       }
  408.       if (--i > 0) s++;        /* get next character */
  409.       else *t++ = c = '\0';    /* end of header */
  410.     }
  411.  
  412.                 /* find header item type */
  413.     if (t = d = strchr (tmp,':')) {
  414.       *d++ = '\0';        /* tie off header item, point at its data */
  415.       while (*d == ' ') d++;    /* flush whitespace */
  416.       while ((tmp < t--) && (*t == ' ')) *t = '\0';
  417.       switch (*ucase (tmp)) {    /* dispatch based on first character */
  418.       case '>':            /* possible >From: */
  419.     if (!strcmp (tmp+1,"FROM")) rfc822_parse_adrlist (&env->from,d,host);
  420.     break;
  421.       case 'B':            /* possible bcc: */
  422.     if (!strcmp (tmp+1,"CC")) rfc822_parse_adrlist (&env->bcc,d,host);
  423.     break;
  424.       case 'C':            /* possible cc: or Content-<mumble>*/
  425.     if (!strcmp (tmp+1,"C")) rfc822_parse_adrlist (&env->cc,d,host);
  426.     else if ((tmp[1] == 'O') && (tmp[2] == 'N') && (tmp[3] == 'T') &&
  427.          (tmp[4] == 'E') && (tmp[5] == 'N') && (tmp[6] == 'T') &&
  428.          (tmp[7] == '-') && body &&
  429.          (MIMEp || (search (s-1,i,"\012MIME-Version",(long) 13))))
  430.       rfc822_parse_content_header (body,tmp+8,d);
  431.     break;
  432.       case 'D':            /* possible Date: */
  433.     if (!env->date && !strcmp (tmp+1,"ATE")) env->date = cpystr (d);
  434.     break;
  435.       case 'F':            /* possible From: */
  436.     if (!strcmp (tmp+1,"ROM")) rfc822_parse_adrlist (&env->from,d,host);
  437.     break;
  438.       case 'I':            /* possible In-Reply-To: */
  439.     if (!env->in_reply_to && !strcmp (tmp+1,"N-REPLY-TO"))
  440.       env->in_reply_to = cpystr (d);
  441.     break;
  442.  
  443.       case 'M':            /* possible Message-ID: or MIME-Version: */
  444.     if (!env->message_id && !strcmp (tmp+1,"ESSAGE-ID"))
  445.       env->message_id = cpystr (d);
  446.     else if (!strcmp (tmp+1,"IME-VERSION")) {
  447.                 /* tie off at end of phrase */
  448.       if (t = rfc822_parse_phrase (d)) *t = '\0';
  449.                 /* known version? */
  450.       if (strcmp (d,"1.0") && strcmp (d,"RFC-XXXX"))
  451.         mm_log ("Warning: message has unknown MIME version",PARSE);
  452.       MIMEp = T;        /* note that we are MIME */
  453.     }
  454.     break;
  455.       case 'N':            /* possible Newsgroups: */
  456.     if (!env->newsgroups && !strcmp (tmp+1,"EWSGROUPS")) {
  457.       t = env->newsgroups = (char *) fs_get (1 + strlen (d));
  458.       while (c = *d++) if (c != ' ' && c != '\t') *t++ = c;
  459.       *t++ = '\0';
  460.     }
  461.     break;
  462.       case 'R':            /* possible Reply-To: */
  463.     if (!strcmp (tmp+1,"EPLY-TO"))
  464.       rfc822_parse_adrlist (&env->reply_to,d,host);
  465.     break;
  466.       case 'S':            /* possible Subject: or Sender: */
  467.     if (!env->subject && !strcmp (tmp+1,"UBJECT"))
  468.       env->subject = cpystr (d);
  469.     else if (!strcmp (tmp+1,"ENDER"))
  470.       rfc822_parse_adrlist (&env->sender,d,host);
  471.     break;
  472.       case 'T':            /* possible To: */
  473.     if (!strcmp (tmp+1,"O")) rfc822_parse_adrlist (&env->to,d,host);
  474.     break;
  475.       default:
  476.     break;
  477.       }
  478.     }
  479.   }
  480.                 /* default Sender: and Reply-To: to From: */
  481.   if (!env->sender) env->sender = rfc822_cpy_adr (env->from);
  482.   if (!env->reply_to) env->reply_to = rfc822_cpy_adr (env->from);
  483.                 /* now parse the body */
  484.   if (body) rfc822_parse_content (body,bs,host,tmp);
  485. }
  486.  
  487. /* Parse a message body content
  488.  * Accepts: pointer to body structure
  489.  *        pointer to body
  490.  *        body byte count
  491.  *        pointer to local host name
  492.  *        pointer to scratch buffer
  493.  */
  494.  
  495. void rfc822_parse_content (body,bs,h,t)
  496.     BODY *body;
  497.     STRING *bs;
  498.     char *h;
  499.     char *t;
  500. {
  501.   char c,c1,*s,*s1;
  502.   unsigned long pos = GETPOS (bs);
  503.   unsigned long i = SIZE (bs);
  504.   unsigned long j,k,m = 0;
  505.   PARAMETER *param;
  506.   PART *part = NIL;
  507.   body->size.ibytes = i;    /* note body size in all cases */
  508.   body->size.bytes = (body->encoding == ENCBASE64 ||
  509.               body->encoding == ENCBINARY) ? i : strcrlflen (bs);
  510.   switch (body->type) {        /* see if anything else special to do */
  511.   case TYPETEXT:        /* text content */
  512.     if (!body->subtype)        /* default subtype */
  513.       body->subtype = cpystr (rfc822_default_subtype (body->type));
  514.     if (!body->parameter) {    /* default parameters */
  515.       body->parameter = mail_newbody_parameter ();
  516.       body->parameter->attribute = cpystr ("charset");
  517.       body->parameter->value = cpystr ("US-ASCII");
  518.     }
  519.                 /* count number of lines */
  520.     while (i--) if ((SNX (bs)) == '\n') body->size.lines++;
  521.     break;
  522.  
  523.   case TYPEMESSAGE:        /* encapsulated message */
  524.     body->contents.msg.env = NIL;
  525.     body->contents.msg.body = NIL;
  526.     body->contents.msg.text = NIL;
  527.     body->contents.msg.offset = pos;
  528.                 /* encapsulated RFC-822 message? */
  529.     if (!strcmp (body->subtype,"RFC822")) {
  530.       if ((body->encoding == ENCBASE64) ||
  531.       (body->encoding == ENCQUOTEDPRINTABLE)
  532.       || (body->encoding == ENCOTHER)) {
  533.     mm_log ("Nested encoding of message contents",PARSE);
  534.     return;
  535.       }
  536.                 /* hunt for blank line */
  537.       for (c = '\012',j = 0; (i > j) && ((c != '\012') || (CHR(bs) != '\012'));
  538.        SNX (bs),j++) if (CHR (bs) != '\015') c = CHR (bs);
  539.       if (i > j) SNX (bs);    /* unless no more text, body starts here */
  540.                 /* note body text offset and header size */
  541.       j = (body->contents.msg.offset = GETPOS (bs)) - pos;
  542.       SETPOS (bs,pos);        /* copy header string */
  543.       s = (char *) fs_get (j + 1);
  544.       for (s1 = s,k = j; k--; *s1++ = SNX (bs));
  545.       s[j] = '\0';        /* tie off string (not really necessary) */
  546.                 /* now parse the body */
  547.       rfc822_parse_msg (&body->contents.msg.env,&body->contents.msg.body,s,j,
  548.             bs,h,t);
  549.       fs_give ((void **) &s);    /* free header string */
  550.       SETPOS (bs,pos);        /* restore position */
  551.     }
  552.                 /* count number of lines */
  553.     while (i--) if (SNX (bs) == '\n') body->size.lines++;
  554.     break;
  555.   case TYPEMULTIPART:        /* multiple parts */
  556.     if ((body->encoding == ENCBASE64) || (body->encoding == ENCQUOTEDPRINTABLE)
  557.     || (body->encoding == ENCOTHER)) {
  558.       mm_log ("Nested encoding of multipart contents",PARSE);
  559.       return;
  560.     }                /* find cookie */
  561.     for (*t = '\0',param = body->parameter; param && !*t; param = param->next)
  562.       if (!strcmp (param->attribute,"BOUNDARY")) strcpy (t,param->value);
  563.     if (!*t) strcpy (t,"-");    /* yucky default */
  564.     j = strlen (t);        /* length of cookie and header */
  565.     c = '\012';            /* initially at beginning of line */
  566.  
  567.     while (i > j) switch (c) {    /* examine each line */
  568.     case '\015':        /* handle CRLF form */
  569.       if (CHR (bs) == '\012') {    /* following LF? */
  570.     c = SNX (bs); i--;    /* yes, slurp it */
  571.       }
  572.     case '\012':        /* at start of a line, start with -- ? */
  573.       m = GETPOS (bs);        /* note start of line */
  574.       if (i-- && ((c = SNX (bs)) == '-') && i-- && ((c = SNX (bs)) == '-')) {
  575.                 /* see if cookie matches */
  576.     for (k = j,s = t; i-- && *s++ == (c = SNX (bs)) && --k;);
  577.     if (k) break;        /* strings didn't match if non-zero */
  578.                 /* look at what follows cookie */
  579.     if (i && i--) switch (c = SNX (bs)) {
  580.     case '-':        /* at end if two dashes */
  581.       if ((i && i--) && ((c = SNX (bs)) == '-') &&
  582.           ((i && i--) ? (((c = SNX (bs)) == '\015') || (c=='\012')) : T)) {
  583.                 /* if have a final part calculate its size */
  584.         if (part && (part->body.size.bytes = m - part->offset))
  585.           part->body.size.bytes--;
  586.         part = NIL; i = 1;    /* terminate scan */
  587.       }
  588.       break;
  589.     case '\015':        /* handle CRLF form */
  590.       if (i && CHR (bs) == '\012') {
  591.         c = SNX (bs); i--;    /* yes, slurp it */
  592.       }
  593.     case '\012':        /* new line */
  594.       if (part) {        /* calculate size of previous */
  595.         part->body.size.bytes = m - part->offset;
  596.                 /* instantiate next */
  597.         part = part->next = mail_newbody_part ();
  598.       }            /* otherwise start new list */
  599.       else part = body->contents.part = mail_newbody_part ();
  600.                 /* note offset from main body */
  601.       part->offset = GETPOS (bs);
  602.       break;
  603.     default:        /* whatever it was it wasn't valid */
  604.       break;
  605.     }
  606.       }
  607.       break;
  608.     default:            /* not at a line */
  609.       c = SNX (bs); i--;    /* get next character */
  610.       break;
  611.     }                /* calculate size of any final part */
  612.     if (part) part->body.size.bytes = GETPOS (bs) - part->offset;
  613.  
  614.                 /* parse body parts */
  615.     for (part = body->contents.part; part; part = part->next) {
  616.                 /* get size of this part, ignore if empty */
  617.       if (i = part->body.size.bytes) {
  618.     SETPOS (bs,part->offset);
  619.                 /* until end of header */
  620.     while (i > 0 && (CHR (bs) != '\012')) {
  621.       s1 = t;        /* initialize buffer pointer */
  622.       c = ' ';        /* and previous character */
  623.       while (c) {        /* collect text until logical end of line */
  624.         switch (c1 = SNX (bs)) {
  625.         case '\012':    /* newline, possible end of logical line */
  626.           if ((i > 0) && (CHR (bs) == '\015')) {
  627.         SNX (bs); i--;    /* eat any CR following */
  628.           }
  629.                 /* tie off unless next line starts with WS */
  630.           if (!i || ((CHR (bs) != ' ') && (CHR(bs) != '\t')))
  631.         *s1 = c = '\0';
  632.           break;
  633.         case '\015':    /* return */
  634.         case '\t':        /* tab */
  635.         case ' ':        /* insert whitespace if not already there */
  636.           if (c != ' ') *s1++ = c = ' ';
  637.           break;
  638.         default:        /* all other characters */
  639.           *s1++ = c = c1;    /* insert the character into the line */
  640.           break;
  641.         }
  642.                 /* end of data ties off the header */
  643.         if (!--i) *s1++ = c = '\0';
  644.       }
  645.                 /* find header item type */
  646.       if (s = strchr (t,':')) {
  647.         *s++ = '\0';    /* tie off header item, point at its data */
  648.                 /* flush whitespace */
  649.         while (*s == ' ') s++;
  650.         if (s1 = strchr (ucase (t),' ')) *s1 = '\0';
  651.         if ((t[0] == 'C') && (t[1] == 'O') && (t[2] == 'N') &&
  652.         (t[3] == 'T') && (t[4] == 'E') && (t[5] == 'N') &&
  653.         (t[6] == 'T') && (t[7] == '-'))
  654.           rfc822_parse_content_header (&part->body,t+8,s);
  655.       }
  656.     }            /* skip trailing (CR)LF */
  657.     if ((i > 0) && (CHR (bs) =='\015')) {i--; SNX (bs);}
  658.     if ((i > 0) && (CHR (bs) =='\012')) {i--; SNX (bs);}
  659.     j = bs->size;        /* save upper level size */
  660.                 /* set size and offset for next level */
  661.     bs->size = (part->offset = GETPOS (bs)) + i;
  662.                 /* now parse it */
  663.     rfc822_parse_content (&part->body,bs,h,t);
  664.     bs->size = j;        /* restore current level size */
  665.       }
  666.  
  667.       else {
  668.     if (!body->subtype)    /* default subtype */
  669.       body->subtype = cpystr (rfc822_default_subtype (body->type));
  670.     if (!body->parameter) {    /* default parameters */
  671.       body->parameter = mail_newbody_parameter ();
  672.       body->parameter->attribute = cpystr ("charset");
  673.       body->parameter->value = cpystr ("US-ASCII");
  674.     }
  675.       }
  676.     }
  677.     break;
  678.   default:            /* nothing special to do in any other case */
  679.     break;
  680.   }
  681. }
  682.  
  683. /* Parse RFC822 body content header
  684.  * Accepts: body to write to
  685.  *        possible content name
  686.  *        remainder of header
  687.  */
  688.  
  689. void rfc822_parse_content_header (body,name,s)
  690.     BODY *body;
  691.     char *name;
  692.     char *s;
  693. {
  694.   PARAMETER *param = NIL;
  695.   char tmp[MAILTMPLEN];
  696.   char c,*t;
  697.   long i;
  698.   switch (*name) {
  699.   case 'I':            /* possible Content-ID */
  700.     if (!(strcmp (name+1,"D") || body->id)) body->id = cpystr (s);
  701.     break;
  702.   case 'D':            /* possible Content-Description */
  703.     if (!(strcmp (name+1,"ESCRIPTION")) || body->description)
  704.       body->description = cpystr (s);
  705.     break;
  706.   case 'T':            /* possible Content-Type/Transfer-Encoding */
  707.     if (!(strcmp (name+1,"YPE") || body->type || body->subtype ||
  708.       body->parameter)) {
  709.                 /* get type word */
  710.       if (!(name = rfc822_parse_word (s,ptspecials))) break;
  711.       c = *name;        /* remember delimiter */
  712.       *name = '\0';        /* tie off type */
  713.       ucase (s);        /* search for body type */
  714.       for (i = 0; (i < TYPEOTHER) && strcmp (s,body_types[i]); i++);
  715.       body->type = i;        /* set body type */
  716.       *name = c;        /* restore delimiter */
  717.       rfc822_skipws (&name);    /* skip whitespace */
  718.       if ((*name == '/') &&    /* subtype? */
  719.       (name = rfc822_parse_word ((s = ++name),ptspecials))) {
  720.     c = *name;        /* save delimiter */
  721.     *name = '\0';        /* tie off subtype */
  722.     rfc822_skipws (&s);    /* copy subtype */
  723.     body->subtype = ucase (cpystr (s ? s :
  724.                        rfc822_default_subtype (body->type)));
  725.     *name = c;        /* restore delimiter */
  726.     rfc822_skipws (&name);    /* skip whitespace */
  727.       }
  728.                 /* subtype defaulting is a no-no, but... */
  729.       else {
  730.     body->subtype = cpystr (rfc822_default_subtype (body->type));
  731.     if (!name) {        /* did the fool have a subtype delimiter? */
  732.       name = s;        /* barf, restore pointer */
  733.       rfc822_skipws (&name);/* skip leading whitespace */
  734.     }
  735.       }
  736.  
  737.                 /* parameter list? */
  738.       while (name && (*name == ';') &&
  739.          (name = rfc822_parse_word ((s = ++name),ptspecials))) {
  740.     c = *name;        /* remember delimiter */
  741.     *name = '\0';        /* tie off attribute name */
  742.     rfc822_skipws (&s);    /* skip leading attribute whitespace */
  743.     if (!*s) *name = c;    /* must have an attribute name */
  744.     else {            /* instantiate a new parameter */
  745.       if (body->parameter) param = param->next = mail_newbody_parameter ();
  746.       else param = body->parameter = mail_newbody_parameter ();
  747.       param->attribute = ucase (cpystr (s));
  748.       *name = c;        /* restore delimiter */
  749.       rfc822_skipws (&name);/* skip whitespace before equal sign */
  750.       if ((*name != '=') ||    /* missing value is a no-no too */
  751.           !(name = rfc822_parse_word ((s = ++name),ptspecials)))
  752.         param->value = cpystr ("UNKNOWN");
  753.       else {        /* good, have equals sign */
  754.         c = *name;        /* remember delimiter */
  755.         *name = '\0';    /* tie off value */
  756.         rfc822_skipws (&s);    /* skip leading value whitespace */
  757.         if (*s) param->value = rfc822_cpy (s);
  758.         *name = c;        /* restore delimiter */
  759.         rfc822_skipws (&name);
  760.       }
  761.     }
  762.       }
  763.       if (!name) {        /* must be end of poop */
  764.     if (param && param->attribute)
  765.       sprintf (tmp,"Missing parameter value: %.80s",param->attribute);
  766.     else strcpy (tmp,"Missing parameter");
  767.     mm_log (tmp,PARSE);
  768.       }
  769.       else if (*name) {        /* must be end of poop */
  770.     sprintf (tmp,"Junk at end of parameters: %.80s",name);
  771.     mm_log (tmp,PARSE);
  772.       }
  773.     }
  774.     else if (!strcmp (name+1,"RANSFER-ENCODING")) {
  775.                 /* flush out any confusing whitespace */
  776.       if (t = strchr (ucase (s),' ')) *t = '\0';
  777.                 /* search for body encoding */
  778.       for (i = 0; (i < ENCOTHER) && strcmp (s,body_encodings[i]); i++);
  779.       body->encoding = i;    /* set body type */
  780.     }
  781.     break;
  782.   default:            /* otherwise unknown */
  783.     break;
  784.   }
  785. }
  786.  
  787. /* Parse RFC822 address list
  788.  * Accepts: address list to write to
  789.  *        input string
  790.  *        default host name
  791.  */
  792.  
  793. void rfc822_parse_adrlist (lst,string,host)
  794.     ADDRESS **lst;
  795.     char *string;
  796.     char *host;
  797. {
  798.   char tmp[MAILTMPLEN];
  799.   char *p,*s;
  800.   long n = 0;
  801.   ADDRESS *last = *lst;
  802.   ADDRESS *adr;
  803.                 /* run to tail of list */
  804.   if (last) while (last->next) last = last->next;
  805.   rfc822_skipws (&string);    /* skip leading WS */
  806.                 /* loop until string exhausted */
  807.   if (*string != '\0') while (p = string) {
  808.                 /* see if start of group */
  809.     while ((*p == ':') || (p = rfc822_parse_phrase (string))) {
  810.       s = p;            /* end of phrase */
  811.       rfc822_skipws (&s);    /* find delimiter */
  812.       if (*s == ':') {        /* really a group? */
  813.     n++;            /* another level */
  814.     *p++ = '\0';        /* tie off group name */
  815.     rfc822_skipws (&p);    /* skip subsequent whitespace */
  816.                 /* write as address */
  817.     (adr = mail_newaddr ())->mailbox = rfc822_cpy (string);
  818.     if (!*lst) *lst = adr;    /* first time through? */
  819.     else last->next = adr;    /* no, append to the list */
  820.     last = adr;        /* set for subsequent linking */
  821.     string = p;        /* continue after this point */
  822.       }
  823.       else break;        /* bust out of this */
  824.     }
  825.     rfc822_skipws (&string);    /* skip any following whitespace */
  826.     if (!string) break;        /* punt if unterminated group */
  827.                 /* if not empty group */
  828.     if (*string != ';' || n <= 0) {
  829.                 /* got an address? */
  830.       if (adr = rfc822_parse_address (&string,host)) {
  831.     if (!*lst) *lst = adr;    /* yes, first time through? */
  832.     else last->next = adr;    /* no, append to the list */
  833.     last = adr;        /* set for subsequent linking */
  834.       }
  835.       else if (string) {    /* bad mailbox */
  836.     sprintf (tmp,"Bad mailbox: %.80s",string);
  837.     mm_log (tmp,PARSE);
  838.     break;
  839.       }
  840.     }
  841.  
  842.                 /* handle end of group */
  843.     if (string && *string == ';' && n >= 0) {
  844.       n--;            /* out of this group */
  845.       string++;            /* skip past the semicolon */
  846.                 /* append end of address mark to the list */
  847.       last->next = (adr = mail_newaddr ());
  848.       last = adr;        /* set for subsequent linking */
  849.       rfc822_skipws (&string);    /* skip any following whitespace */
  850.       switch (*string) {    /* see what follows */
  851.       case ',':            /* another address? */
  852.     ++string;        /* yes, skip past the comma */
  853.       case ';':            /* another end of group? */
  854.       case '\0':        /* end of string */
  855.     break;
  856.       default:
  857.     sprintf (tmp,"Junk at end of group: %.80s",string);
  858.     mm_log (tmp,PARSE);
  859.     break;
  860.       }
  861.     }
  862.   }
  863.   while (n-- > 0) {        /* if unterminated groups */
  864.     last->next = (adr = mail_newaddr ());
  865.     last = adr;            /* set for subsequent linking */
  866.   }
  867. }
  868.  
  869. /* Parse RFC822 address
  870.  * Accepts: pointer to string pointer
  871.  *        default host
  872.  * Returns: address
  873.  *
  874.  * Updates string pointer
  875.  */
  876.  
  877. ADDRESS *rfc822_parse_address (string,defaulthost)
  878.     char **string;
  879.     char *defaulthost;
  880. {
  881.   char tmp[MAILTMPLEN];
  882.   ADDRESS *adr;
  883.   char c,*s;
  884.   char *phrase;
  885.   if (!string) return NIL;
  886.   rfc822_skipws (string);    /* flush leading whitespace */
  887.  
  888.   /* This is much more complicated than it should be because users like
  889.    * to write local addrspecs without "@localhost".  This makes it very
  890.    * difficult to tell a phrase from an addrspec!
  891.    * The other problem we must cope with is a route-addr without a leading
  892.    * phrase.  Yuck!
  893.    */
  894.  
  895.   if (*(s = *string) == '<')     /* note start, handle case of phraseless RA */
  896.     adr = rfc822_parse_routeaddr (s,string,defaulthost);
  897.   else {            /* get phrase if any */
  898.     if ((phrase = rfc822_parse_phrase (s)) &&
  899.     (adr = rfc822_parse_routeaddr (phrase,string,defaulthost))) {
  900.       *phrase = '\0';        /* tie off phrase */
  901.                 /* phrase is a personal name */
  902.       adr->personal = rfc822_cpy (s);
  903.     }
  904.     else adr = rfc822_parse_addrspec (s,string,defaulthost);
  905.   }
  906.                 /* analyze what follows */
  907.   if (*string) switch (c = **string) {
  908.   case ',':            /* comma? */
  909.     ++*string;            /* then another address follows */
  910.     break;
  911.   case ';':            /* possible end of group? */
  912.     break;            /* let upper level deal with it */
  913.   default:
  914.     s = isalnum (c) ? "Must use comma to separate addresses: %.80s" :
  915.       "Junk at end of address: %.80s";
  916.     sprintf (tmp,s,*string);
  917.     mm_log (tmp,PARSE);
  918.                 /* falls through */
  919.   case '\0':            /* null-specified address? */
  920.     *string = NIL;        /* punt remainder of parse */
  921.     break;
  922.   }
  923.   return adr;            /* return the address */
  924. }
  925.  
  926. /* Parse RFC822 route-address
  927.  * Accepts: string pointer
  928.  *        pointer to string pointer to update
  929.  * Returns: address
  930.  *
  931.  * Updates string pointer
  932.  */
  933.  
  934. ADDRESS *rfc822_parse_routeaddr (string,ret,defaulthost)
  935.     char *string;
  936.     char **ret;
  937.     char *defaulthost;
  938. {
  939.   char tmp[MAILTMPLEN];
  940.   ADDRESS *adr;
  941.   char *adl = NIL;
  942.   char *routeend = NIL;
  943.   if (!string) return NIL;
  944.   rfc822_skipws (&string);    /* flush leading whitespace */
  945.                 /* must start with open broket */
  946.   if (*string != '<') return NIL;
  947.   if (string[1] == '@') {    /* have an A-D-L? */
  948.     adl = ++string;        /* yes, remember that fact */
  949.     while (*string != ':') {    /* search for end of A-D-L */
  950.                 /* punt if never found */
  951.       if (*string == '\0') return NIL;
  952.       ++string;            /* try next character */
  953.     }
  954.     *string = '\0';        /* tie off A-D-L */
  955.     routeend = string;        /* remember in case need to put back */
  956.   }
  957.                 /* parse address spec */
  958.   if (!(adr = rfc822_parse_addrspec (++string,ret,defaulthost))) {
  959.     if (adl) *routeend = ':';    /* put colon back since parse barfed */
  960.     return NIL;
  961.   }
  962.                 /* have an A-D-L? */
  963.   if (adl) adr->adl = cpystr (adl);
  964.                 /* make sure terminated OK */
  965.     if (*ret) if (**ret == '>') {
  966.     ++*ret;            /* skip past the broket */
  967.     rfc822_skipws (ret);    /* flush trailing WS */
  968.                 /* wipe pointer if at end of string */
  969.     if (**ret == '\0') *ret = NIL;
  970.     return adr;            /* return the address */
  971.   }
  972.   sprintf (tmp,"Unterminated mailbox: %.80s@%.80s",adr->mailbox,adr->host);
  973.   mm_log (tmp,PARSE);
  974.   return adr;            /* return the address */
  975. }
  976.  
  977. /* Parse RFC822 address-spec
  978.  * Accepts: string pointer
  979.  *        pointer to string pointer to update
  980.  *        default host
  981.  * Returns: address
  982.  *
  983.  * Updates string pointer
  984.  */
  985.  
  986. ADDRESS *rfc822_parse_addrspec (string,ret,defaulthost)
  987.     char *string;
  988.     char **ret;
  989.     char *defaulthost;
  990. {
  991.   ADDRESS *adr;
  992.   char *end;
  993.   char c,*s,*t;
  994.   if (!string) return NIL;
  995.   rfc822_skipws (&string);    /* flush leading whitespace */
  996.                 /* find end of mailbox */
  997.   if (!(end = rfc822_parse_word (string,NIL))) return NIL;
  998.   adr = mail_newaddr ();    /* create address block */
  999.   c = *end;            /* remember delimiter */
  1000.   *end = '\0';            /* tie off mailbox */
  1001.                 /* copy mailbox */
  1002.   adr->mailbox = rfc822_cpy (string);
  1003.   *end = c;            /* restore delimiter */
  1004.   t = end;            /* remember end of mailbox for no host case */
  1005.   rfc822_skipws (&end);        /* skip whitespace */
  1006.   if (*end == '@') {        /* have host name? */
  1007.     ++end;            /* skip delimiter */
  1008.     rfc822_skipws (&end);    /* skip whitespace */
  1009.     *ret = end;            /* update return pointer */
  1010.                     /* search for end of host */
  1011.     if (end = rfc822_parse_word ((string = end),hspecials)) {
  1012.       c = *end;            /* remember delimiter */
  1013.       *end = '\0';        /* tie off host */
  1014.                 /* copy host */
  1015.       adr->host = rfc822_cpy (string);
  1016.       *end = c;            /* restore delimiter */
  1017.     }
  1018.     else mm_log ("Missing or invalid host name after @",PARSE);
  1019.   }
  1020.   else end = t;            /* make person name default start after mbx */
  1021.  
  1022.                 /* default host if missing */
  1023.   if (!adr->host) adr->host = cpystr (defaulthost);
  1024.   if (end && !adr->personal) {    /* try person name in comments if missing */
  1025.     while (*end == ' ') ++end;    /* see if we can find a person name here */
  1026.                 /* found something that may be a name? */
  1027.     if ((*end == '(') && (t = s = rfc822_parse_phrase (end + 1))) {
  1028.       rfc822_skipws (&s);    /* heinous kludge for trailing WS in comment */
  1029.       if (*s == ')') {        /* look like good end of comment? */
  1030.     *t = '\0';        /* yes, tie off the likely name and copy */
  1031.     ++end;            /* skip over open paren now */
  1032.     rfc822_skipws (&end);
  1033.     adr->personal = rfc822_cpy (end);
  1034.     end = ++s;        /* skip after it */
  1035.       }
  1036.     }
  1037.     rfc822_skipws (&end);    /* skip any other WS in the normal way */
  1038.   }
  1039.                 /* set return to end pointer */
  1040.   *ret = (end && *end) ? end : NIL;
  1041.   return adr;            /* return the address we got */
  1042. }
  1043.  
  1044. /* Parse RFC822 phrase
  1045.  * Accepts: string pointer
  1046.  * Returns: pointer to end of phrase
  1047.  */
  1048.  
  1049. char *rfc822_parse_phrase (s)
  1050.     char *s;
  1051. {
  1052.   char *curpos;
  1053.   if (!s) return NIL;        /* no-op if no string */
  1054.                 /* find first word of phrase */
  1055.   curpos = rfc822_parse_word (s,NIL);
  1056.   if (!curpos) return NIL;    /* no words means no phrase */
  1057.                 /* check if string ends with word */
  1058.   if (*curpos == '\0') return curpos;
  1059.   s = curpos;            /* sniff past the end of this word and WS */
  1060.   rfc822_skipws (&s);        /* skip whitespace */
  1061.                 /* recurse to see if any more */
  1062.   return (s = rfc822_parse_phrase (s)) ? s : curpos;
  1063. }
  1064.  
  1065. /* Parse RFC822 word
  1066.  * Accepts: string pointer
  1067.  * Returns: pointer to end of word
  1068.  */
  1069.  
  1070. char *rfc822_parse_word (s,delimiters)
  1071.     char *s;
  1072.     const char *delimiters;
  1073. {
  1074.   char *st,*str;
  1075.   if (!s) return NIL;        /* no-op if no string */
  1076.   rfc822_skipws (&s);        /* flush leading whitespace */
  1077.   if (*s == '\0') return NIL;    /* end of string */
  1078.                 /* default delimiters to standard */
  1079.   if (!delimiters) delimiters = wspecials;
  1080.   str = s;            /* hunt pointer for strpbrk */
  1081.   while (T) {            /* look for delimiter */
  1082.     if (!(st = strpbrk (str,delimiters))) {
  1083.       while (*s != '\0') ++s;    /* no delimiter, hunt for end */
  1084.       return s;            /* return it */
  1085.     }
  1086.     switch (*st) {        /* dispatch based on delimiter */
  1087.     case '"':            /* quoted string */
  1088.                 /* look for close quote */
  1089.       while (*++st != '"') switch (*st) {
  1090.       case '\0':        /* unbalanced quoted string */
  1091.     return NIL;        /* sick sick sick */
  1092.       case '\\':        /* quoted character */
  1093.     ++st;            /* skip the next character */
  1094.       default:            /* ordinary character */
  1095.     break;            /* no special action */
  1096.       }
  1097.       str = ++st;        /* continue parse */
  1098.       break;
  1099.     case '\\':            /* quoted character */
  1100.       str = st + 2;        /* skip quoted character and go on */
  1101.       break;
  1102.     default:            /* found a word delimiter */
  1103.       return (st == s) ? NIL : st;
  1104.     }
  1105.   }
  1106. }
  1107.  
  1108. /* Copy an RFC822 format string
  1109.  * Accepts: string
  1110.  * Returns: copy of string
  1111.  */
  1112.  
  1113. char *rfc822_cpy (src)
  1114.     char *src;
  1115. {
  1116.                 /* copy and unquote */
  1117.   return rfc822_quote (cpystr (src));
  1118. }
  1119.  
  1120.  
  1121. /* Unquote an RFC822 format string
  1122.  * Accepts: string
  1123.  * Returns: string
  1124.  */
  1125.  
  1126. char *rfc822_quote (src)
  1127.     char *src;
  1128. {
  1129.   char *ret = src;
  1130.   if (strpbrk (src,"\\\"")) {    /* any quoting in string? */
  1131.     char *dst = ret;
  1132.     while (*src) {        /* copy string */
  1133.       if (*src == '\"') src++;    /* skip double quote entirely */
  1134.       else {
  1135.     if (*src == '\\') src++;/* skip over single quote, copy next always */
  1136.     *dst++ = *src++;    /* copy character */
  1137.       }
  1138.     }
  1139.     *dst = '\0';        /* tie off string */
  1140.   }
  1141.   return ret;            /* return our string */
  1142. }
  1143.  
  1144.  
  1145. /* Copy address list
  1146.  * Accepts: address list
  1147.  * Returns: address list
  1148.  */
  1149.  
  1150. ADDRESS *rfc822_cpy_adr (adr)
  1151.     ADDRESS *adr;
  1152. {
  1153.   ADDRESS *dadr;
  1154.   ADDRESS *ret = NIL;
  1155.   ADDRESS *prev = NIL;
  1156.   while (adr) {            /* loop while there's still an MAP adr */
  1157.     dadr = mail_newaddr ();    /* instantiate a new address */
  1158.     if (!ret) ret = dadr;    /* note return */
  1159.     if (prev) prev->next = dadr;/* tie on to the end of any previous */
  1160.     dadr->personal = cpystr (adr->personal);
  1161.     dadr->adl = cpystr (adr->adl);
  1162.     dadr->mailbox = cpystr (adr->mailbox);
  1163.     dadr->host = cpystr (adr->host);
  1164.     prev = dadr;        /* this is now the previous */
  1165.     adr = adr->next;        /* go to next address in list */
  1166.   }
  1167.   return (ret);            /* return the MTP address list */
  1168. }
  1169.  
  1170. /* Skips RFC822 whitespace
  1171.  * Accepts: pointer to string pointer
  1172.  */
  1173.  
  1174. void rfc822_skipws (s)
  1175.     char **s;
  1176. {
  1177.   char tmp[MAILTMPLEN];
  1178.   char *t;
  1179.   long n = 0;
  1180.                 /* while whitespace or start of comment */
  1181.   while ((**s == ' ') || (n = (**s == '('))) {
  1182.     t = *s;            /* note comment pointer */
  1183.     if (**s == '(') while (n) {    /* while comment in effect */
  1184.       switch (*++*s) {        /* get character of comment */
  1185.       case '(':            /* nested comment? */
  1186.     n++;            /* increment nest count */
  1187.     break;
  1188.       case ')':            /* end of comment? */
  1189.     n--;            /* decrement nest count */
  1190.     break;
  1191.       case '"':            /* quoted string? */
  1192.     while (*++*s != '\"') if (!**s || (**s == '\\' && !*++*s)) {
  1193.       sprintf (tmp,"Unterminated quoted string within comment: %.80s",t);
  1194.       mm_log (tmp,PARSE);
  1195.       *t = '\0';        /* nuke duplicate messages in case reparse */
  1196.       return;
  1197.     }
  1198.     break;
  1199.       case '\\':        /* quote next character? */
  1200.     if (*++*s != '\0') break;
  1201.       case '\0':        /* end of string */
  1202.     sprintf (tmp,"Unterminated comment: %.80s",t);
  1203.     mm_log (tmp,PARSE);
  1204.     *t = '\0';        /* nuke duplicate messages in case reparse */
  1205.     return;            /* this is wierd if it happens */
  1206.       default:            /* random character */
  1207.     break;
  1208.       }
  1209.     }
  1210.     ++*s;            /* skip past whitespace character */
  1211.   }
  1212. }
  1213.  
  1214. /* Body contents utility and encoding/decoding routines */
  1215.  
  1216.  
  1217. /* Return body contents in normal form
  1218.  * Accepts: pointer to destination
  1219.  *        pointer to length of destination
  1220.  *        returned destination length
  1221.  *        source
  1222.  *        length of source
  1223.  *        source encoding
  1224.  * Returns: destination
  1225.  *
  1226.  * Originally, this routine was supposed to do decoding as well, but that was
  1227.  * moved to a higher level.  Now, it's merely a jacket into strcrlfcpy that
  1228.  * avoids the work for BASE64 and BINARY segments.
  1229.  */
  1230.  
  1231. char *rfc822_contents (dst,dstl,len,src,srcl,encoding)
  1232.     char **dst;
  1233.     unsigned long *dstl;
  1234.     unsigned long *len;
  1235.                     char *src;
  1236.     unsigned long srcl;
  1237.     unsigned short encoding;
  1238. {
  1239.   *len = 0;            /* in case we return an error */
  1240.   switch (encoding) {        /* act based on encoding */
  1241.   case ENCBASE64:        /* 3 to 4 binary */
  1242.   case ENCBINARY:        /* unmodified binary */
  1243.     if ((*len = srcl) > *dstl) {/* resize if not enough space */
  1244.       fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  1245.       *dst = (char *) fs_get ((*dstl = srcl) + 1);
  1246.     }
  1247.     memcpy (*dst,src,srcl);    /* copy that many bytes */
  1248.     *(*dst + srcl) = '\0';    /* tie off destination */
  1249.     break;
  1250.   default:            /* all other cases return strcrlfcpy version */
  1251.     *len = strlen (*dst = strcrlfcpy (dst,dstl,src,srcl));
  1252.     break;
  1253.   }
  1254.   return *dst;            /* return the string */
  1255. }
  1256.  
  1257.  
  1258. /* Output RFC 822 message
  1259.  * Accepts: temporary buffer
  1260.  *        envelope
  1261.  *        body
  1262.  *        I/O routine
  1263.  *        stream for I/O routine
  1264.  * Returns: T if successful, NIL if failure
  1265.  */
  1266.  
  1267. long rfc822_output (t,env,body,f,s)
  1268.     char *t;
  1269.     ENVELOPE *env;
  1270.     BODY *body;
  1271.     soutr_t f;
  1272.     TCPSTREAM *s;
  1273. {
  1274.   rfc822_encode_body (env,body);/* encode body as necessary */
  1275.   rfc822_header (t,env,body);    /* build RFC822 header */
  1276.                 /* output header and body */
  1277.   return (*f) (s,t) && (body ? rfc822_output_body (body,f,s) : T);
  1278. }
  1279.  
  1280. /* Encode a body for 7BIT transmittal
  1281.  * Accepts: envelope
  1282.  *        body
  1283.  */
  1284.  
  1285. void rfc822_encode_body (env,body)
  1286.     ENVELOPE *env;
  1287.     BODY *body;
  1288. {
  1289.   void *f;
  1290.   PART *part;
  1291.   if (body) switch (body->type) {
  1292.   case TYPEMULTIPART:        /* multi-part */
  1293.     if (!body->parameter) {    /* cookie not set up yet? */
  1294.       char tmp[MAILTMPLEN];    /* make cookie not in BASE64 or QUOTEPRINT*/
  1295.       sprintf (tmp,"%ld-%ld-%ld:#%ld",gethostid (),random (),time (0),
  1296.            getpid ());
  1297.       body->parameter = mail_newbody_parameter ();
  1298.       body->parameter->attribute = cpystr ("BOUNDARY");
  1299.       body->parameter->value = cpystr (tmp);
  1300.     }
  1301.     part = body->contents.part;    /* encode body parts */
  1302.     do rfc822_encode_body (env,&part->body);
  1303.     while (part = part->next);    /* until done */
  1304.     break;
  1305.   case TYPEMESSAGE:        /* encapsulated message */
  1306.     if (!((body->encoding == ENC7BIT) || (body->encoding == ENC8BIT) ||
  1307.       (body->encoding == ENCBINARY)))
  1308.       fatal ("Invalid rfc822_encode_body message encoding");
  1309.     break;            /* can't change encoding */
  1310.   default:            /* all else has some encoding */
  1311.     switch (body->encoding) {
  1312.     case ENC8BIT:        /* encode 8BIT into QUOTED-PRINTABLE */
  1313.                 /* remember old 8-bit contents */
  1314.       f = (void *) body->contents.text;
  1315.       body->contents.text = rfc822_8bit (body->contents.text,body->size.bytes,
  1316.                      &body->size.bytes);
  1317.       body->encoding = ENCQUOTEDPRINTABLE;
  1318.       fs_give (&f);        /* flush old binary contents */
  1319.       break;
  1320.     case ENCBINARY:        /* encode binary into BASE64 */
  1321.       f = body->contents.binary;/* remember old binary contents */
  1322.       body->contents.text = rfc822_binary (body->contents.binary,
  1323.                        body->size.bytes,&body->size.bytes);
  1324.       body->encoding = ENCBASE64;
  1325.       fs_give (&f);        /* flush old binary contents */
  1326.     default:            /* otherwise OK */
  1327.       break;
  1328.     }
  1329.     break;
  1330.   }
  1331. }
  1332.  
  1333. /* Output RFC 822 body
  1334.  * Accepts: body
  1335.  *        I/O routine
  1336.  *        stream for I/O routine
  1337.  * Returns: T if successful, NIL if failure
  1338.  */
  1339.  
  1340. long rfc822_output_body (body,f,s)
  1341.     BODY *body;
  1342.     soutr_t f;
  1343.     TCPSTREAM *s;
  1344. {
  1345.   PART *part;
  1346.   PARAMETER *param;
  1347.   char *cookie = NIL;
  1348.   char tmp[MAILTMPLEN];
  1349.   char *t;
  1350.   switch (body->type) {
  1351.   case TYPEMULTIPART:        /* multipart gets special handling */
  1352.     part = body->contents.part;    /* first body part */
  1353.                 /* find cookie */
  1354.     for (param = body->parameter; param && !cookie; param = param->next)
  1355.       if (!strcmp (param->attribute,"BOUNDARY")) cookie = param->value;
  1356.     if (!cookie) cookie = "-";    /* yucky default */
  1357.     do {            /* for each part */
  1358.                 /* build cookie */
  1359.       sprintf (t = tmp,"--%s\015\012",cookie);
  1360.                 /* append mini-header */
  1361.       rfc822_write_body_header (&t,&part->body);
  1362.       strcat (t,"\015\012");    /* write terminating blank line */
  1363.                 /* output cookie, mini-header, and contents */
  1364.       if (!((*f) (s,tmp) && rfc822_output_body (&part->body,f,s))) return NIL;
  1365.     } while (part = part->next);/* until done */
  1366.                 /* output trailing cookie */
  1367.     sprintf (t = tmp,"--%s--",cookie);
  1368.     break;
  1369.   case TYPEMESSAGE:        /* encapsulated message */
  1370.     t = body->contents.msg.text;
  1371.     break;
  1372.   default:            /* all else is text now */
  1373.     t = (char *) body->contents.text;
  1374.     break;
  1375.   }
  1376.                 /* output final stuff */
  1377.   if (t && *t && !((*f) (s,t) && (*f) (s,"\015\012"))) return NIL;
  1378.   return LONGT;
  1379. }
  1380.  
  1381. /* Convert BASE64 contents to binary
  1382.  * Accepts: source
  1383.  *        length of source
  1384.  *        pointer to return destination length
  1385.  * Returns: destination as binary
  1386.  */
  1387.  
  1388. void *rfc822_base64 (src,srcl,len)
  1389.     unsigned char *src;
  1390.     unsigned long srcl;
  1391.     unsigned long *len;
  1392. {
  1393.   char c;
  1394.   void *ret = fs_get (4 + ((srcl * 3) / 4));
  1395.   char *d = (char *) ret;
  1396.   short e = 0;
  1397.   *len = 0;            /* in case we return an error */
  1398.   while (srcl--) {        /* until run out of characters */
  1399.     c = *src++;            /* simple-minded decode */
  1400.     if (isupper (c)) c -= 'A';
  1401.     else if (islower (c)) c -= 'a' - 26;
  1402.     else if (isdigit (c)) c -= '0' - 52;
  1403.     else if (c == '+') c = 62;
  1404.     else if (c == '/') c = 63;
  1405.     else if (c == '=') {    /* padding */
  1406.       switch (e++) {        /* check quantum position */
  1407.       case 2:
  1408.     if (*src != '=') return NIL;
  1409.     break;
  1410.       case 3:
  1411.     e = 0;            /* restart quantum */
  1412.     break;
  1413.       default:            /* impossible quantum position */
  1414.     fs_give (&ret);
  1415.     return NIL;
  1416.       }
  1417.       continue;
  1418.     }
  1419.     else continue;        /* junk character */
  1420.     switch (e++) {        /* install based on quantum position */
  1421.     case 0:
  1422.       *d = c << 2;        /* byte 1: high 6 bits */
  1423.       break;
  1424.     case 1:
  1425.       *d++ |= c >> 4;        /* byte 1: low 2 bits */
  1426.       *d = c << 4;        /* byte 2: high 4 bits */
  1427.       break;
  1428.     case 2:
  1429.       *d++ |= c >> 2;        /* byte 2: low 4 bits */
  1430.       *d = c << 6;        /* byte 3: high 2 bits */
  1431.       break;
  1432.     case 3:
  1433.       *d++ |= c;        /* byte 3: low 6 bits */
  1434.       e = 0;            /* reinitialize mechanism */
  1435.       break;
  1436.     }
  1437.   }
  1438.   *len = d - (char *) ret;    /* calculate data length */
  1439.   return ret;            /* return the string */
  1440. }
  1441.  
  1442. /* Convert binary contents to BASE64
  1443.  * Accepts: source
  1444.  *        length of source
  1445.  *        pointer to return destination length
  1446.  * Returns: destination as BASE64
  1447.  */
  1448.  
  1449. unsigned char *rfc822_binary (src,srcl,len)
  1450.     void *src;
  1451.     unsigned long srcl;
  1452.     unsigned long *len;
  1453. {
  1454.   unsigned char *ret,*d;
  1455.   unsigned char *s = (unsigned char *) src;
  1456.   char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  1457.   unsigned long i = ((srcl + 2) / 3) * 4;
  1458.   *len = i += 2 * ((i / 60) + 1);
  1459.   d = ret = (unsigned char *) fs_get (++i);
  1460.   for (i = 0; srcl; s += 3) {    /* process tuplets */
  1461.     *d++ = v[s[0] >> 2];    /* byte 1: high 6 bits (1) */
  1462.                 /* byte 2: low 2 bits (1), high 4 bits (2) */
  1463.     *d++ = v[((s[0] << 4) + (--srcl ? (s[1] >> 4) : 0)) & 0x3f];
  1464.                 /* byte 3: low 4 bits (2), high 2 bits (3) */
  1465.     *d++ = srcl ? v[((s[1] << 2) + (--srcl ? (s[2] >> 6) : 0)) & 0x3f] : '=';
  1466.                 /* byte 4: low 6 bits (3) */
  1467.     *d++ = srcl ? v[s[2] & 0x3f] : '=';
  1468.     if (srcl) srcl--;        /* count third character if processed */
  1469.     if ((++i) == 15) {        /* output 60 characters? */
  1470.       i = 0;            /* restart line break count, insert CRLF */
  1471.       *d++ = '\015'; *d++ = '\012';
  1472.     }
  1473.   }
  1474.   *d++ = '\015'; *d++ = '\012';    /* insert final CRLF */
  1475.   *d = '\0';            /* tie off string */
  1476.   if ((d - ret) != *len) fatal ("rfc822_binary logic flaw");
  1477.   return ret;            /* return the resulting string */
  1478. }
  1479.  
  1480. /* Convert QUOTED-PRINTABLE contents to 8BIT
  1481.  * Accepts: source
  1482.  *        length of source
  1483.  *         pointer to return destination length
  1484.  * Returns: destination as 8-bit text
  1485.  */
  1486.  
  1487. unsigned char *rfc822_qprint (src,srcl,len)
  1488.     unsigned char *src;
  1489.     unsigned long srcl;
  1490.                        unsigned long *len;
  1491. {
  1492.   unsigned char *ret = (unsigned char *) fs_get (srcl);
  1493.   unsigned char *d = ret;
  1494.   unsigned char *s = d;
  1495.   unsigned char c,e;
  1496.   *len = 0;            /* in case we return an error */
  1497.   src[srcl] = '\0';        /* make sure string tied off */
  1498.   while (c = *src++) {        /* until run out of characters */
  1499.     switch (c) {        /* what type of character is it? */
  1500.     case '=':            /* quoting character */
  1501.       switch (c = *src++) {    /* what does it quote? */
  1502.       case '\0':        /* end of data */
  1503.     src--;            /* back up pointer */
  1504.     break;
  1505.       case '\015':        /* non-significant line break */
  1506.     if (*src == '\012') src++;
  1507.     break;
  1508.       default:            /* two hex digits then */
  1509.     if (!isxdigit (c)) {    /* must be hex! */
  1510.       fs_give ((void **) &ret);
  1511.       return NIL;
  1512.     }
  1513.     if (isdigit (c)) e = c - '0';
  1514.     else e = c - (isupper (c) ? 'A' - 10 : 'a' - 10);
  1515.     c = *src++;        /* snarf next character */
  1516.     if (!isxdigit (c)) {    /* must be hex! */
  1517.       fs_give ((void **) &ret);
  1518.       return NIL;
  1519.     }
  1520.     if (isdigit (c)) c -= '0';
  1521.     else c -= (isupper (c) ? 'A' - 10 : 'a' - 10);
  1522.     *d++ = c + (e << 4);    /* merge the two hex digits */
  1523.     s = d;            /* note point of non-space */
  1524.     break;
  1525.       }
  1526.       break;
  1527.     case ' ':            /* space, possibly bogus */
  1528.       *d++ = c;            /* stash the space but don't update s */
  1529.       break;
  1530.     case '\015':        /* end of line */
  1531.       d = s;            /* slide back to last non-space, drop in */
  1532.     default:
  1533.       *d++ = c;            /* stash the character */
  1534.       s = d;            /* note point of non-space */
  1535.     }
  1536.   }
  1537.   *d = '\0';            /* tie off results */
  1538.   *len = d - ret;        /* calculate length */
  1539.   return ret;            /* return the string */
  1540. }
  1541.  
  1542. /* Convert 8BIT contents to QUOTED-PRINTABLE
  1543.  * Accepts: source
  1544.  *        length of source
  1545.  *         pointer to return destination length
  1546.  * Returns: destination as quoted-printable text
  1547.  */
  1548.  
  1549. #define MAXL 75            /* 76th position only used by continuation = */
  1550.  
  1551. unsigned char *rfc822_8bit (src,srcl,len)
  1552.     unsigned char *src;
  1553.     unsigned long srcl;
  1554.                      unsigned long *len;
  1555. {
  1556.   unsigned long lp = 0;
  1557.   unsigned char *ret = (unsigned char *) fs_get (3*srcl + srcl/MAXL + 2);
  1558.   unsigned char *d = ret;
  1559.   char *hex = "0123456789ABCDEF";
  1560.   unsigned char c;
  1561.   while (srcl--) {        /* for each character */
  1562.                 /* true line break? */
  1563.     if (((c = *src++) == '\015') && (*src == '\012') && srcl) {
  1564.       *d++ = '\015'; *d++ = *src++; srcl--;
  1565.       lp = 0;            /* reset line count */
  1566.     }
  1567.     else {            /* not a line break */
  1568.                 /* quoting required? */
  1569.       if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') ||
  1570.       ((c == ' ') && (*src == '\015'))) {
  1571.     if ((lp += 3) > MAXL) {    /* yes, would line overflow? */
  1572.       *d++ = '='; *d++ = '\015'; *d++ = '\012';
  1573.       lp = 3;        /* set line count */
  1574.     }
  1575.     *d++ = '=';        /* quote character */
  1576.     *d++ = hex[c >> 4];    /* high order 4 bits */
  1577.     *d++ = hex[c & 0xf];    /* low order 4 bits */
  1578.       }
  1579.       else {            /* ordinary character */
  1580.     if ((++lp) > MAXL) {    /* would line overflow? */
  1581.       *d++ = '='; *d++ = '\015'; *d++ = '\012';
  1582.       lp = 1;        /* set line count */
  1583.     }
  1584.     *d++ = c;        /* ordinary character */
  1585.       }
  1586.     }
  1587.   }
  1588.   *d = '\0';            /* tie off destination */
  1589.   *len = d - ret;        /* calculate true size */
  1590.                 /* try to give some space back */
  1591.   fs_resize ((void **) &ret,*len);
  1592.   return ret;
  1593. }
  1594.